package com.jack.thinking.java._22.swing.listener;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import com.utils.SwingConsole;

public class MyTrackEvent extends JFrame{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Map<String, JTextField> m = new HashMap<>();
	private String[] event = {
			"focusGained", "focusLost", "keyPressed",
			"keyReleased", "keyTyped", "mouseClicked",
			"mouseEntered", "mouseExited", "mousePressed",
			"mouseReleased", "mouseDragged", "mouseMoved"
	};
	private MyButton b1 = new MyButton(Color.BLUE, "test1"),
				     b2 = new MyButton(Color.RED, "test2");
	
	class MyButton extends JButton{
		private static final long serialVersionUID = 1L;
		
		FocusListener fl = null;
		KeyListener kl = null;
		MouseListener ml = null;
		MouseMotionListener mml = null;
		
		{
			listeners();
		}
		
		void report(String field , String msg) {
			m.get(field).setText(msg);
		}
		
		public MyButton(Color color, String label) {
			super(label);
			setBackground(color);
			addFocusListener(fl);
			addKeyListener(kl);
			addMouseListener(ml);
			addMouseMotionListener(mml);
		}
		
		public void listeners(){
			fl = new FocusListener() {
				
				@Override
				public void focusLost(FocusEvent e) {
					report(event[1], e.paramString());
				}
				
				@Override
				public void focusGained(FocusEvent e) {
					report(event[0], e.paramString());
				}
			};
			
			kl = new KeyListener() {

				/**
				 * 键盘键入 有效内容，而非控制键
				 */
				@Override
				public void keyTyped(KeyEvent e) {
					System.out.println(e.getKeyCode());	
					report(event[4], e.paramString());
				}

				@Override
				public void keyPressed(KeyEvent e) {
					System.out.println(e.getKeyCode());	
					report(event[2], e.paramString());
				}

				@Override
				public void keyReleased(KeyEvent e) {
					report(event[3], e.paramString());				
				}
				
			};
			
			ml = new MouseListener() {
				
				@Override
				public void mouseReleased(MouseEvent e) {
					report(event[9], e.paramString());
				}
				
				@Override
				public void mousePressed(MouseEvent e) {
					report(event[8], e.paramString());
				}
				
				@Override
				public void mouseExited(MouseEvent e) {
					report(event[7], e.paramString());
				}
				
				@Override
				public void mouseEntered(MouseEvent e) {
					report(event[6], e.paramString());					
				}
				
				@Override
				public void mouseClicked(MouseEvent e) {
					report(event[5], e.paramString());					
				}
			};
			
			mml = new MouseMotionListener() {
				@Override
				public void mouseMoved(MouseEvent e) {
					report(event[11], e.paramString());
				}
				
				@Override
				public void mouseDragged(MouseEvent e) {
					report(event[10], e.paramString());
				}
			};
		}
	}
	
	public MyTrackEvent() {
		setLayout(new GridLayout(event.length + 1, 2));
		
		for(String evt : event) {
			JTextField t = new JTextField();
			t.setEditable(false);
			add(new JLabel(evt, JLabel.RIGHT));
			add(t);
			m.put(evt, t);
		}
		add(b1);
		add(b2);
	}
	
	public static void main(String[] args) {
		SwingConsole.run(new MyTrackEvent(), 1000, 500);
	}
}
